<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: BaseService.php
 */

namespace App\Services;

/**
 * Class BaseService
 * @package App\Services
 */
abstract class BaseService
{
    protected static $container = [];

    /**
     * 获取instance
     * @param mixed $args
     * @return static
     */
    public static function getInstance($args = null)
    {
        $hash = self::hash($args);
        if (isset(static::$container[$hash])) {
            return static::$container[$hash];
        }
        if ($args == null) {
            static::$container[$hash] = new static();
        } else {
            static::$container[$hash] = new static($args);
        }
        return static::$container[$hash];
    }

    /**
     * hash
     * @param $args
     * @return string
     */
    protected static function hash($args): string
    {
        return md5(static::class . json_encode($args));
    }

}
